var flagFlower = [];
    $("#main").on("click", ".r-h-review-btn", function () {
        var _id = $(this).attr("_id")
        var arr_key = $(this).attr("arr_key")
        if (_id == 1) {
            $(this).find("img").attr("src", "/static/index/img/icon-goodReview-selected.png");
            $(this).siblings().find("img").attr("src", "/static/index/img/icon-badReview.png");
            $(this).find("span").addClass("thison")
            $(this).siblings().find("span").removeClass("thison")

        } else if (_id == 2) {
            $(this).find("img").attr("src", "/static/index/img/icon-badReview-selected.png");
            $(this).siblings(".r-h-badreview").find("img").attr("src", "/static/index/img/icon-badReview.png");
            $(this).siblings(".r-h-goodreview").find("img").attr("src", "/static/index/img/icon-goodReview.png");
            $(this).find("span").addClass("thison")
            $(this).siblings().find("span").removeClass("thison")
        } else if (_id == 3) {
            $(this).find("img").attr("src", "/static/index/img/icon-badReview-selected.png");
            $(this).siblings(".r-h-middlereview").find("img").attr("src", "/static/index/img/icon-badReview.png");
            $(this).siblings(".r-h-goodreview").find("img").attr("src", "/static/index/img/icon-goodReview.png");
            $(this).find("span").addClass("thison")
            $(this).siblings().find("span").removeClass("thison")
        }
        flagFlower[arr_key] = _id;
        console.log(flagFlower);
    })
    $("#main").on("click", ".userinfo-avatar", function () {
        $(this).attr("src", "/static/index/img/starselected.png")
        $(this).prevAll().attr("src", "/static/index/img/starselected.png")
        $(this).nextAll().attr("src", "/static/index/img/star.png")
        var _id = $(this).attr('data-id');
        $(this).siblings('.flag').val(_id);
    })
    var orderid = getUrlParam("id")
    var imgArr = [];
    var getData = [];
    $.ajax({
        type: "get",
        url: Url + 'order/evaluate/' + orderid,
        async: false,
        timeout : 20000,
        beforeSend: function () {
            //xhr.setRequestHeader("token",storage.getItem('token'));
        },
        success: function (data) {
            console.log(data);
            getData = data.getData;
            console.log(getData.length);
            var order_id = data.order_id;
            $(".order_id").val(order_id);

            var str = '';
            for (var i = 0; i < getData.length; i++) {
                imgArr[i] = [];
                str += '<div>'
                    + '<div class="review-header clear">'
                    + '<div class="r-h-img"><img src="' + getData[i].main_img_url + '"/></div>'
                    + '<div class="r-h-review clear">'
                    + '<div class="r-h-goodreview r-h-review-btn" _id="1" arr_key="' + i + '">'
                    + '<img src="/static/index/img/icon-goodReview.png"/>'
                    + '<span>好评</span>'
                    + '</div>'
                    + '<div class="r-h-middlereview r-h-review-btn" _id="2" arr_key="' + i + '">'
                    + '<img src="/static/index/img/icon-badReview.png"/>'
                    + '<span>中评</span>'
                    + '</div>'
                    + '<div class="r-h-badreview r-h-review-btn" _id="3" arr_key="' + i + '">'
                    + '<img src="/static/index/img/icon-badReview.png"/>'
                    + '<span>差评</span>'
                    + '</div>'
                    + '</div>'
                    + '</div>'
                    + '<div class="review-main1"   arr_key="' + i + '">'
                    + '<textarea placeholder="宝贝满足你的要求吗？说说你的使用心得,分享给想买的他们吧" name="evaluate[]" class="evaluate_text"></textarea>'
                    + '<div class="addimg clear">'
                    + '<div class="uploadDiv">'
                    + '<img class="file_img" src="/static/index/img/addimg.png" />'
                    + '<input class="addimgIpt" type="file" name="images" />'
                    + '<input type="hidden" name="img[]" class="proimg" value=""/>'
                    + '<span class="deleteImg">×</span>'
                    + '</div>'
                    + '</div>'
                    + '</div>'
                    + '</div>'
            }
            $("#myupload").prepend(str)
        },
        error: function (request, status, err) {
            failure(status)
        }
    });

    function deleteValue(str, arr, value) {
        var arrNum = $.inArray(str, arr)
        if (arrNum > -1) {
            if (value) {
                arr.splice(arrNum, 1, value);
            } else {
                arr.splice(arrNum, 1);
            }
        }
    }

    $("#main").on("click", ".deleteImg", function () {
        var uploadDivs = $(this).parent().siblings();
        var thisfu = $(this).parents(".addimg")
        var img_value = $(this).prev().val();
        var qy = $(this).parents('.review-main1').attr('arr_key');
        deleteValue(img_value, imgArr[qy]);
        console.log(imgArr);
        $(this).parent().remove();
        for (var i = 0; i < uploadDivs.length; i++) {
            console.log(uploadDivs.eq(i).children(".proimg").val())
            if (uploadDivs.eq(i).children(".proimg").val() == "") {
                return;
            }
        }
        var sttr = '<div class="uploadDiv">'
            + '<img class="file_img" src="/static/index/img/addimg.png"/>'
            + '<input  class="addimgIpt" type="file" name="images"/>'
            + '<input type="hidden" name="img[]" class="proimg" value=""/>'
            + '<span class="deleteImg">×</span>'
            + '</div>'
        thisfu.append(sttr);

    })

    function adduploadDiv(that) {
        var uploadDiv_num = $(that).parent().siblings().length + 1;
        if (uploadDiv_num <= 2) {
            var sttr = '<div class="uploadDiv">'
                + '<img class="file_img" src="/static/index/img/addimg.png"/>'
                + '<input  class="addimgIpt" type="file" name="images"/>'
                + '<input type="hidden" name="img[]" class="proimg" value=""/>'
                + '<span class="deleteImg">×</span>'
                + '</div>';
            $(that).parents(".addimg").append(sttr)
        }
    }

    $("#main").on("change", ".addimgIpt", function () {
        var that = this;
        var qy = $(that).parents('.review-main1').attr('arr_key');
        var img_value = $(this).next().val();
        $('.is_whether').val(img_value);
        $('.qyClass').val(qy);
        $("#myupload").ajaxSubmit({
            dataType: 'json', //数据格式为json
            beforeSend: function () { //开始上传
            },
            uploadProgress: function (event, position, total, percentComplete) {
            },
            success: function (data) {
                console.log(data);

                if (data.state == 1) {
                    var src = data.path;
                    $(that).prev().attr('src', src);
                    $(that).next(".proimg").val(src);
                    $(that).siblings(".deleteImg").css("display", "block");
                    adduploadDiv(that)
                    if (data.is_whether) {
                        deleteValue(data.is_whether, imgArr[data.qy], data.path)
                    } else {
                        imgArr[data.qy].push(data.path);
                    }
                    console.log(imgArr);
                    //$(".res").html("上传图片"+data.name+"成功，图片大小："+data.size+"K,文件地址："+data.url);
                } else {
                    $(".res").html(data.errmsg);
                }
            },
            error: function (xhr,status) { //上传失败
                alert("上传失败");
                progress.hide();
                failure(status)
            }
        });
    })
    $('.save').on('click', function () {
        var postData = {};
        var arr = [];
        for (var i = 0; i < $(".evaluate_text").length; i++) {
            arr.push($(".evaluate_text").eq(i).val())
        }
        postData['img'] = imgArr;
        postData['flagFlower'] = flagFlower;
        var flag = [];
        for (var i = 0; i < $(".flag").length; i++) {
            flag.push($(".flag").eq(i).val())
        }
        postData['flag1'] = flag[0];
        postData['flag2'] = flag[1];
        postData['flag3'] = flag[2];
        postData['orderid'] = $(".order_id").val();
        postData['evaluate'] = arr;
        var Info = {};
        Info['postInfo'] = getData;
        Info['postData'] = postData;
        console.log(Info);
        $.ajax({
            type: "post",
            url: Url + 'postEvaluate',
            data: Info,
            async: false,
            dataType: 'json',
            timeout : 20000,
            beforeSend: function (xhr) {

                    showDiv()

                xhr.setRequestHeader("token", tokenk)
            },
            success: function (data) {
                hiddenDiv()
                if (data.state) {
                    var flag = true;
                    location.href = 'pay_result.html?id=' + data.order_id + '&flag=' + flag + '&from=pj';
                } else {
                    var flag = false;
                    location.href = 'pay_result.html?id=' + data.order_id + '&flag=' + flag + '&from=pj';
                }
            },
            error: function (request, status, err) {
                failure(status)
            }
        })


    });

